home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / src / igl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  3.5 KB  |  142 lines

  1. #include <ad709/tinygl/igl.h>
  2. #include "zgl.h"
  3.  
  4. typedef struct {
  5.     GLContext *gl_context;
  6.     int xsize,ysize;
  7.     ave_win_t *drawable;
  8.     ave_pix_t *gc;
  9.     int draw;
  10. } TinyIGLContext;
  11.  
  12.  
  13.  
  14. ///////////////// Private aux functions
  15.  
  16. static int igl_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr);
  17. static int igl_init(IGLContext ctx1, int xsize, int ysize);
  18.  
  19.  
  20. static int igl_init(IGLContext ctx1, int xsize, int ysize) {
  21.     TinyIGLContext *ctx = (TinyIGLContext *) ctx1;
  22.     int mode;
  23.     ZBuffer *zb;
  24.     
  25.     if (ctx->gl_context == NULL) {
  26.         /* currently, we only support 16 bit rendering */
  27.         mode = ZB_MODE_5R6G5B;
  28.         zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
  29.         if (zb == NULL) {
  30.             fprintf(stderr, "Error while initializing Z buffer\n");
  31.             exit(1);
  32.         }
  33.         /* initialisation of the TinyGL interpreter */
  34.         glInit(zb);
  35.         ctx->gl_context=gl_get_context();    
  36.         ctx->gl_context->opaque=(void *) ctx;
  37.         ctx->gl_context->gl_resize_viewport=igl_resize_viewport;
  38.         
  39.         /* set the viewport : we force a call to glX_resize_viewport */
  40.         ctx->gl_context->viewport.xsize=-1;
  41.         ctx->gl_context->viewport.ysize=-1;
  42.         //glViewport(0, 0, xsize, ysize);
  43.     }
  44.     
  45.     return 1;
  46. }
  47.  
  48.  
  49.  
  50. /* resize the glx viewport : we try to use the xsize and ysize
  51. given. We return the effective size which is guaranted to be smaller */
  52.  
  53. static int igl_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr) {
  54.     TinyIGLContext *ctx;
  55.     int xsize,ysize;
  56.     
  57.     ctx=(TinyIGLContext *)c->opaque;
  58.     
  59.     xsize=*xsize_ptr;
  60.     ysize=*ysize_ptr;
  61.     
  62.     /* we ensure that xsize and ysize are multiples of 2 for the zbuffer. 
  63. TODO: find a better solution */
  64.     xsize&=~3;
  65.     ysize&=~3;
  66.     
  67.     if (xsize == 0 || ysize == 0) return -1;
  68.     
  69.     *xsize_ptr=xsize;
  70.     *ysize_ptr=ysize;
  71.     
  72.     ctx->xsize=xsize;
  73.     ctx->ysize=ysize;
  74.     
  75.     /* resize the Z buffer */
  76.     ZB_resize(c->zb,NULL,xsize,ysize);
  77.     return 0;
  78. }
  79.  
  80.  
  81.  
  82. ////////////////////////// igl API functions
  83.  
  84.  
  85. IGLContext iglCreateContext() {
  86.     TinyIGLContext *ctx;
  87.     
  88.     ctx=malloc(sizeof(TinyIGLContext));
  89.     if (!ctx)
  90.         return NULL;
  91.     ctx->gl_context = NULL;
  92.     igl_init(ctx, 0, 0);
  93.     return (IGLContext) ctx;
  94. }
  95.  
  96.  
  97. void iglDestroyContext( IGLContext ctx1 ) {
  98.     TinyIGLContext *ctx = (TinyIGLContext *) ctx1;
  99.     if (ctx->gl_context != NULL) {
  100.         glClose();
  101.     }
  102.     free(ctx);
  103. }
  104.  
  105.  
  106.  
  107. void iglMakeCurrent(ave_win_t *drawable, IGLContext ctx1) {
  108.     TinyIGLContext *ctx = (TinyIGLContext *) ctx1;    
  109.     ave_dialog_gadgets_t win_gadgets;
  110.     ave_avo_size_t win_size;
  111.     int xsize, ysize;
  112.     
  113.     win_size = ave_avo_getsize((ave_avo_t *) drawable);        
  114.     xsize = win_size.width;
  115.     ysize = win_size.height;
  116.     igl_resize_viewport(ctx->gl_context, &xsize, &ysize);
  117.     ctx->gc = ave_pix_16bit_open(ctx->gl_context->zb->pbuf, xsize, ysize, xsize*2);
  118.     win_gadgets = ave_dialog_getgadgets(drawable);
  119.     ave_avo_add(win_gadgets.content, (ave_avo_t *) ctx->gc, 0);
  120.     ctx->gl_context->viewport.xsize=-1;
  121.     ctx->gl_context->viewport.ysize=-1;
  122. }
  123.  
  124.  
  125.  
  126. void iglResizeContext(IGLContext ctx1, int width, int height, ave_win_t *drawable) {
  127.     ave_dialog_gadgets_t win_gadgets;
  128.     TinyIGLContext *ctx = (TinyIGLContext *) ctx1;
  129.     win_gadgets = ave_dialog_getgadgets(drawable);
  130.     ave_obj_deref((ave_obj_t *) ctx->gc);
  131.     ave_avo_sub(win_gadgets.content, (ave_avo_t *) ctx->gc);
  132.     ctx->gc = ave_pix_16bit_open(ctx->gl_context->zb->pbuf, width, height, width*2);
  133.     ave_avo_add(win_gadgets.content, (ave_avo_t *) ctx->gc, 0);
  134. }
  135.  
  136.  
  137. ave_pix_t *iglGetPixmap(IGLContext ctx1) {
  138.     TinyIGLContext *ctx = (TinyIGLContext *) ctx1;
  139.     return ctx->gc;
  140. }
  141.  
  142.